<?php

/**
 * Auth identity object
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    modules
 * @subpackage admin
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Identity.php 47 2009-05-28 17:56:56Z monzee $
 */
class Admin_Model_Auth_Identity extends ArrayObject implements Zend_Acl_Role_Interface
{
    /**
     * ARRAY_AS_PROPS by default.
     * 
     * @param mixed  $data
     * @param int    $flags
     * @param string $iterator Iterator class
     */
    public function __construct($data = array(), 
                                $flags = ArrayObject::ARRAY_AS_PROPS,
                                $iterator = "ArrayIterator")
    {
        parent::__construct($data, $flags, $iterator);
    }

    /**
     * Zend_Acl_Role_Interface method
     *
     * @return string
     */
    public function getRoleId()
    {
        return $this->offsetGet('username');
    }

    /**
     * Returns true if roles contain $roleId
     *
     * @param mixed $roleId A single role id or an array of ids
     * @return bool
     * @todo This should use Zend_Acl::inheritsRole() so that the whole heirarchy
     *       is queried, not just the immediate parent roles.
     */
    public function isA($roleId)
    {
        if (!$this->offsetExists('roles')) {
            return false;
        }
        $roles = $this->offsetGet('roles');
        // $roles is expected to be an array of arrays
        $roleId = (array) $roleId;
        foreach ($roles as $role) {
            if (in_array($role['id'], $roleId)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Alias for grammar pedants ;)
     *
     * @param mixed $roleId role id or array of role ids
     * @return bool
     */
    public function isAn($roleId)
    {
        return $this->isA($roleId);
    }

}